package com.meixiang.activity.account.manager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.shopping.VerifyGoodsOrderActivity;
import com.meixiang.entity.account.AddressEntity;
import com.meixiang.entity.account.AddressListEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.address.WheelViewAddressDialog;
import com.meixiang.view.wheel.AddressDialog;
import com.meixiang.view.wheel.mode.ProvinceModel;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 新增、编辑地址
 * author：wangzuzhen
 */
public class AddressActivity extends BaseActivity {

    /**
     * 收货人
     */
    @Bind(R.id.et_input_consignee)
    ClearEditText mEtInputConsignee;
    @Bind(R.id.tv_consignee)
    TextView mTvConsignee;
    @Bind(R.id.rl_consignee)
    RelativeLayout mRlConsignee;
    @Bind(R.id.tv_contact_phone)
    TextView mTvContactPhone;
    /**
     * 联系电话
     */
    @Bind(R.id.et_input_contact_phone)
    ClearEditText mEtInputContactPhone;
    @Bind(R.id.rl_contact_phone)
    RelativeLayout mRlContactPhone;
    /**
     * 所在地区
     */
    @Bind(R.id.tv_district)
    TextView mTvDistrict;
    @Bind(R.id.et_input_district)
    TextView mEtInputDistrict;
    @Bind(R.id.imageView)
    ImageView mImageView;
    @Bind(R.id.rl_district)
    RelativeLayout mRlDistrict;
    /**
     * 详细地址
     */
    @Bind(R.id.tv_detail_address)
    TextView mTvDetailAddress;
    @Bind(R.id.et_input_detail_address)
    ClearEditText mEtInputDetailAddress;
    @Bind(R.id.rl_detail_address)
    RelativeLayout mRlDetailAddress;
    /**
     * 邮政编码
     */
    @Bind(R.id.et_input_postalcode)
    ClearEditText mEtInputPostalcode;
    @Bind(R.id.tv_postalcode)
    TextView mTvPostalcode;
    @Bind(R.id.rl_postalcode)
    RelativeLayout mRlPostalcode;
    /**
     * 保存
     */
    @Bind(R.id.tv_affirm)
    TextView mTvAffirm;

    String status;//1是编辑，2是新增
    String addressId;
    boolean fromOrder; // true表示是从确认订单界面进来的
    String provinceIds;//省
    String cityIds;//市
    String districtIds;//区


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTitle() {
        setTitle(getResources().getString(R.string.add_address));
        if (getIntent() != null) {
            status = getIntent().getStringExtra("status");//1是编辑，2是新增
            addressId = getIntent().getStringExtra("addressId");//1是编辑，2是新增
            fromOrder = getIntent().getBooleanExtra("fromOrder", false);
            if (status.equals("1")) {
                setTitle(getResources().getString(R.string.redact_address));
                getAddressEdit();
            }
        }
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void setData() {

    }

    @OnClick({R.id.rl_district, R.id.tv_affirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_district:
                new WheelViewAddressDialog(this, "所在地区", "完成", new WheelViewAddressDialog.IResultListener() {
                    @Override
                    public void RightTitleOnclick(AddressEntity proEntity, AddressEntity cityEntity, AddressEntity disEntity) {
                        String pro = "";
                        String city = "";
                        String dis = "";
                        if (proEntity != null) {
                            pro = proEntity.areaName;
                            provinceIds = proEntity.areaId;
                        } else {
                            provinceIds = null;
                        }
                        if (cityEntity != null) {
                            city = cityEntity.areaName;
                            cityIds = cityEntity.areaId;
                        } else {
                            cityIds = null;
                        }
                        if (disEntity != null) {
                            dis = disEntity.areaName;
                            districtIds = disEntity.areaId;
                        } else {
                            districtIds = null;
                        }
                        mEtInputDistrict.setText(new StringBuilder().append(pro).append(" ").append(city).append(" ").append(dis));
                    }
                });

                break;
            case R.id.tv_affirm:
                if (TextUtils.isEmpty(mEtInputConsignee.getText().toString())) {
                    AbToastUtil.showToast(context, "请输入收货人姓名");
                    return;
                }
                if (TextUtils.isEmpty(mEtInputContactPhone.getText().toString())) {
                    AbToastUtil.showToast(context, "请输入联系方式");
                    return;
                }
                if (TextUtils.isEmpty(mEtInputDistrict.getText().toString())) {
                    AbToastUtil.showToast(context, "请选择所在城市、地区");
                    return;
                }
                if (TextUtils.isEmpty(mEtInputDetailAddress.getText().toString())) {
                    AbToastUtil.showToast(context, "请输入街道、楼牌号");
                    return;
                }
                if (TextUtils.isEmpty(mEtInputPostalcode.getText().toString())) {
                    AbToastUtil.showToast(context, "请输入收货地编码");
                    return;
                }
                if (status.equals("2") || fromOrder) {//status--1是编辑，2是新增
                    // 新增
                    addAddress();
                } else {
                    // 更新
                    getAddressUpdate();
                }
                break;
        }
    }

    //编辑
    private void getAddressEdit() {
        HttpParams params = new HttpParams();
        params.put("addressId", addressId);
        HttpUtils.post(Config.MY_ADDRESS_EDIT, params, new HttpCallBack(AddressActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if ("0".equals(code)) {
                    mEtInputPostalcode.setText(result.optString("zipCode"));
                    mEtInputDetailAddress.setText(result.optString("address"));
                    mEtInputDistrict.setText(result.optString("areaInfo"));
                    mEtInputContactPhone.setText(result.optString("mobPhone"));
                    mEtInputConsignee.setText(result.optString("trueName"));
                    provinceIds = result.optString("provinceId");
                    cityIds = result.optString("cityId");
                    districtIds = result.optString("areaId");
                } else
                    AbToastUtil.showToast(context, message);
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });
    }
    //保存

    /**
     * addressId	String	必填			地址ID
     * zipCode	String	必填			邮政编码
     * address	String	必填			详细地址
     * areaInfo	String	必填			省城市区区中文描述，以，号分开
     * cityId	String	必填			区ID
     * areaId	String	必填			城市ID
     * provinceId	String	必填			省ID
     * mobPhone	String	必填			手机
     * trueName	String	必填			真实姓名
     */
    private void getAddressUpdate() {
        HttpParams params = new HttpParams();
        params.put("addressId", addressId);
        params.put("zipCode", mEtInputPostalcode.getText().toString());
        params.put("address", mEtInputDetailAddress.getText().toString());
        params.put("areaInfo", mEtInputDistrict.getText().toString());
        params.put("cityId", cityIds);
        params.put("areaId", districtIds);
        params.put("provinceId", provinceIds);
        params.put("mobPhone", mEtInputContactPhone.getText().toString());
        params.put("trueName", mEtInputConsignee.getText().toString());
        HttpUtils.post(Config.MY_ADDRESS_UPDATE, params, new HttpCallBack(AddressActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if ("0".equals(code)) {
                    finish();
                    AbToastUtil.showToast(context, message);
                }
                AbToastUtil.showToast(context, message);
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });
    }

    /**
     * 新增地址
     */
    private void addAddress() {
        HttpParams params = new HttpParams();
        params.put("zipCode", mEtInputPostalcode.getText().toString());
        params.put("address", mEtInputDetailAddress.getText().toString());
        params.put("areaInfo", mEtInputDistrict.getText().toString());
        params.put("cityId", cityIds);
        params.put("areaId", districtIds);
        params.put("provinceId", provinceIds);
        params.put("mobPhone", mEtInputContactPhone.getText().toString());
        params.put("trueName", mEtInputConsignee.getText().toString());
        HttpUtils.post(Config.MY_ADDRESS_SAVE, params, new HttpCallBack(AddressActivity.this) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (status.equals("2") || fromOrder) {
                    Intent intent = new Intent();
                    AddressListEntity address = AbJsonUtil.fromJson(result.toString(), AddressListEntity.class);
                    intent.putExtra(VerifyGoodsOrderActivity.ADDRESS, address);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(context, message);
            }
        });
    }
}
